const shareUtil = require('../../../utils/shareUtil.js');
var app = getApp();
let imgUrl=app.globalData.imgUrl
Page({
    data: {
        diceList: [ {
            imgUrl: "/images/common/dice/dice-1.svg",
            bgColor: "red"
        }, {
            imgUrl: "/images/common/dice/dice-2.svg",
            bgColor: "blue"
        }, {
            imgUrl: "/images/common/dice/dice-3.svg",
            bgColor: "blue"
        }, {
            imgUrl: "/images/common/dice/dice-4.svg",
            bgColor: "red"
        }, {
            imgUrl: "/images/common/dice/dice-5.svg",
            bgColor: "blue"
        } ],
        result: [ 6, 6, 6, 6, 6 ],
        isHidden: false,
        openHit: false
    },
    onLoad: function(e) {},
    mask: function() {
        this.setData({
            isHidden: !this.data.isHidden
        });
    },
    playGame: function() {
        for (var e = [], i = 0; i < 5; i++) e[i] = Math.round(5 * Math.random() + 1);
        this.setData({
            result: e
        }), 
        this.playMusic();
    },
    getResult: function() {
        for (var e = [], i = this.data.result, o = 0; o < 5; o++) {
            var n = "blue";
            1 != i[o] && 4 != i[o] || (n = "red");
            var t = {
                imgUrl: "/images/common/dice/dice-" + i[o] + ".svg",
                bgColor: n
            };
            e.push(t);
        }
        this.setData({
            diceList: e
        });
    },
    playMusic: function() {
        var that = this,
        i = wx.createInnerAudioContext();
        i.src = imgUrl+"/shaizi.mp3",
        i.play(), 
        i.onEnded(function(i) {
          that.getResult();
        }), i.onError(function(i) {
          that.getResult();
        });
    },
    onReady: function() {},
    onShow: function() {
        var that = this;
        this.openHit = true, wx.onAccelerometerChange(function(i) {
          that.openHit && (i.x > .5 && i.y > .5 || i.x > .5 && i.z > .5 || i.z > .5 && i.y > .5) && that.playGame();
        });
    },
    onHide: function() {
        this.openHit = false;
    },
    onUnload: function() {
        this.openHit = false;
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});